////////////////////////////////////////////////////////////////////////////
//	CameCore.h & GameCore.cpp
//		Class that handles core behavior of the game, containing functions
//		to initialize, update, render and shutdown the game. Since there
//		should always be only one instance of this class, it's a singleton.
//		Most of its functions are called by WinMain.
//
//	Author:					Daniel Lima - March 2012
/////////////////////////////////////////////////////////////////////////////

#include "GameCore.h"
#include "Input\InputManager.h"
#include "Graphics\D2DGraphicsWrapper.h"
#include "Util\VectorMath.h"

// Initialize GameCore's static instance
GameCore* GameCore::sm_pInstance = nullptr;

// Constructor - Initialize all variables to default values
GameCore::GameCore(void)
{
	m_hWnd			= nullptr;
	m_nScreenWidth	= 0;
	m_nScreenHeight = 0;
	m_bFullScreen	= false;
	m_pCurrentState	= nullptr;

	nTestImg = -1;
}


// Used to access the singleton's instance
GameCore* GameCore::GetInstance(void)
{
	if(!sm_pInstance)
		sm_pInstance = new GameCore;

	return sm_pInstance;
}

// Used to delete the singleton's instance
void GameCore::DeleteInstance(void)
{
	if(sm_pInstance)
	{
		sm_pInstance->Shutdown();
		delete sm_pInstance;
		sm_pInstance = nullptr;
	}
}

// Initialize the game engines with the right window parameters
bool GameCore::Initialize(HWND hWnd, HINSTANCE hInstance, int nScreenWidth, int nScreenHeight, bool bIsWindowed)
{
	// Bool to check for failures in the initialization
	HRESULT hResult = true;

	// Initialize Direct2D
	D2DGraphicsWrapper::GetInstance()->Initialize(hWnd);

	// Register this class to receive input
	hResult = InputManager::GetInstance()->Register(this);
	if(!hResult) return false;

	// Tests
	InputManager::GetInstance()->BindKey(VK_LBUTTON, COMMAND_WALK_RIGHT);
	InputManager::GetInstance()->BindKey('B', COMMAND_WALK_LEFT);
	InputManager::GetInstance()->BindKey('C', COMMAND_ATTACK);

	nTestImg = D2DGraphicsWrapper::GetInstance()->Load(_T("resource/graphics/test_image.png"), 0, 0);

	////////

	// If we got here we succeeded
	return true;
}

// Run the main game loop
bool GameCore::Main(void)
{
	Update();
	Render();

	return true;
}

// Update the game based on the current state and calculate game time
void GameCore::Update(void)
{
}

// Render the game based on the current state
void GameCore::Render(void)
{
	// TESTS
	D2DGraphicsWrapper::GetInstance()->StartDrawing();
	D2DGraphicsWrapper::GetInstance()->Draw(nTestImg, 0, 0);
	D2DGraphicsWrapper::GetInstance()->StopDrawing(m_hWnd);
}

// Function called every time an input message is sent by the InputManager
void GameCore::HandleInput(const InputMessage * const pInputMsg)
{
	switch(pInputMsg->GetID())
	{
	case COMMAND_WALK_RIGHT:
		LOG("Key bound to ID Right was pressed");
		break;
	case COMMAND_WALK_LEFT:
		LOG("Key bound to ID Left was pressed");
		break;
	case COMMAND_ATTACK:
		LOG("Key bound to ID Attack was pressed");
		break;
	}
}

// Shutdown the game engines
void GameCore::Shutdown(void)
{
	// Unregister this class to receive input
	InputManager::GetInstance()->Unregister(this);

	// Shutdown Direct2D
	D2DGraphicsWrapper::GetInstance()->Shutdown();
}